
package com.ld.admin.service.webbot;

import org.apache.commons.lang.StringUtils;

import com.eastrobot.ask.sdk.AskRequest;
import com.eastrobot.ask.sdk.AskResponse;
import com.eastrobot.ask.sdk.AskService;
import com.eastrobot.ask.sdk.CloudServiceFactory;
import com.eastrobot.ask.utils.Constant;

import lombok.extern.slf4j.Slf4j;

/**
 * 小i机器人
 * 
 * @ClassName IBotService
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年8月29日 上午11:44:24
 *
 */
@Slf4j
public class IBotService {

    /**
     * Chat with IBot Robot.
     *
     * @param msg
     *            the specified message
     * @return robot returned message, return {@code null} if not found
     */
    public String chat(String msg) {
        if (StringUtils.isBlank(msg)) {
            return null;
        }

        if (StringUtils.isBlank(msg)) {
            msg = "你好~";
        }

        /**
         * ITPK Robot API.
         */
        String key = WebBotConfig.getString("ibot.key");
        /**
         * ITPK Robot Key.
         */
        String secret = WebBotConfig.getString("ibot.secret");

        try {
            /**
             * 返回类型:Constant.PRIMARY_TYPE/Constant.SENIOR_TYPE
             */
            String type = Constant.PRIMARY_TYPE;
            /**
             * 平台:请见Constant类后缀为_PLATFORM的常量
             */
            String platform = Constant.WEB_PLATFORM;
            String userId = "请填写userId";

            // 智能问答
            AskRequest askRequest = new AskRequest(key, secret, msg, type, userId, platform);
            AskService askService = CloudServiceFactory.getInstance().createAskService();
            askService.init(null);
            AskResponse askResponse = null;
            askResponse = askService.ask(askRequest);

            return askResponse.getContent();
        } catch (Exception e) {
            log.error("Chat with Robot failed", e);
        }

        return null;
    }
}
